package com.eshop.tools.utils;

import org.apache.commons.logging.LogFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.util.Map;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.core.type.TypeReference;
import java.util.List;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;

public class JacksonUtil
{
    private static final Log logger;
    
    public static String parseString(final String body, final String field) {
        final ObjectMapper mapper = new ObjectMapper();
        try {
            final JsonNode node = mapper.readTree(body);
            final JsonNode leaf = node.get(field);
            if (leaf != null) {
                return leaf.asText();
            }
        }
        catch (IOException e) {
            JacksonUtil.logger.error((Object)e.getMessage(), (Throwable)e);
        }
        return null;
    }
    
    public static List<String> parseStringList(final String body, final String field) {
        final ObjectMapper mapper = new ObjectMapper();
        try {
            final JsonNode node = mapper.readTree(body);
            final JsonNode leaf = node.get(field);
            if (leaf != null) {
                return (List<String>)mapper.convertValue((Object)leaf, (TypeReference)new TypeReference<List<String>>() {});
            }
        }
        catch (IOException e) {
            JacksonUtil.logger.error((Object)e.getMessage(), (Throwable)e);
        }
        return null;
    }
    
    public static Integer parseInteger(final String body, final String field) {
        final ObjectMapper mapper = new ObjectMapper();
        try {
            final JsonNode node = mapper.readTree(body);
            final JsonNode leaf = node.get(field);
            if (leaf != null) {
                return leaf.asInt();
            }
        }
        catch (IOException e) {
            JacksonUtil.logger.error((Object)e.getMessage(), (Throwable)e);
        }
        return null;
    }
    
    public static List<Integer> parseIntegerList(final String body, final String field) {
        final ObjectMapper mapper = new ObjectMapper();
        try {
            final JsonNode node = mapper.readTree(body);
            final JsonNode leaf = node.get(field);
            if (leaf != null) {
                return (List<Integer>)mapper.convertValue((Object)leaf, (TypeReference)new TypeReference<List<Integer>>() {});
            }
        }
        catch (IOException e) {
            JacksonUtil.logger.error((Object)e.getMessage(), (Throwable)e);
        }
        return null;
    }
    
    public static Boolean parseBoolean(final String body, final String field) {
        final ObjectMapper mapper = new ObjectMapper();
        try {
            final JsonNode node = mapper.readTree(body);
            final JsonNode leaf = node.get(field);
            if (leaf != null) {
                return leaf.asBoolean();
            }
        }
        catch (IOException e) {
            JacksonUtil.logger.error((Object)e.getMessage(), (Throwable)e);
        }
        return null;
    }
    
    public static Short parseShort(final String body, final String field) {
        final ObjectMapper mapper = new ObjectMapper();
        try {
            final JsonNode node = mapper.readTree(body);
            final JsonNode leaf = node.get(field);
            if (leaf != null) {
                final Integer value = leaf.asInt();
                return value.shortValue();
            }
        }
        catch (IOException e) {
            JacksonUtil.logger.error((Object)e.getMessage(), (Throwable)e);
        }
        return null;
    }
    
    public static Byte parseByte(final String body, final String field) {
        final ObjectMapper mapper = new ObjectMapper();
        try {
            final JsonNode node = mapper.readTree(body);
            final JsonNode leaf = node.get(field);
            if (leaf != null) {
                final Integer value = leaf.asInt();
                return value.byteValue();
            }
        }
        catch (IOException e) {
            JacksonUtil.logger.error((Object)e.getMessage(), (Throwable)e);
        }
        return null;
    }
    
    public static <T> T parseObject(final String body, final String field, final Class<T> clazz) {
        final ObjectMapper mapper = new ObjectMapper();
        try {
            JsonNode node = mapper.readTree(body);
            node = node.get(field);
            return (T)mapper.treeToValue((TreeNode)node, (Class)clazz);
        }
        catch (IOException e) {
            JacksonUtil.logger.error((Object)e.getMessage(), (Throwable)e);
            return null;
        }
    }
    
    public static Object toNode(final String json) {
        if (json == null) {
            return null;
        }
        final ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.readTree(json);
        }
        catch (IOException e) {
            JacksonUtil.logger.error((Object)e.getMessage(), (Throwable)e);
            return null;
        }
    }
    
    public static Map<String, String> toMap(final String data) {
        final ObjectMapper objectMapper = new ObjectMapper();
        try {
            return (Map<String, String>)objectMapper.readValue(data, (TypeReference)new TypeReference<Map<String, String>>() {});
        }
        catch (IOException e) {
            JacksonUtil.logger.error((Object)e.getMessage(), (Throwable)e);
            return null;
        }
    }
    
    public static String toJson(final Object data) {
        final ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(data);
        }
        catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    static {
        logger = LogFactory.getLog((Class)JacksonUtil.class);
    }
}
